<?php
namespace common\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/02/04
 */

/**
 * Order model
 * @property integer $id
 * @property string $uid
 * @property integer $status
 * @property integer $deleted
 * @property integer $ctime
 * @property string $contact_ids
 * @property string $ext
 * confirm
 */
class Order extends Base {
    public static function tableName() {
        return 'dive_order';
    }

    const NOT_DELETED=0;
    const DELETED = 1;
    const ONE_SELF=1;//独占
    const NOT_ONE_SELF=0;//共享

    public static function getDetail($orderId) {
        if (empty($orderId)) {
            return [
                'err' => 1,
                'msg' => 'orderId不能为空'
            ];
        }
        $uid = Yii::$app->user->getId();
        $order = self::findOne(['order_id' => $orderId]);
        $ext = json_decode($order->ext, true);
        if (empty($order) || $order->uid != $uid) {
            return [
                'err' => 1,
                'msg' => '订单不存在'
            ];
        }
        $contact = [];
        if (!empty($order->contact_ids)) {
            $contacts = Contact::getList($order->contact_ids);
            foreach ($contacts as $k => $v) {
                $contact[] = [
                    'name' => $v['name'],
                    'gender' => $v['sex']
                ];
            }
        }
        $statusFlow = OrderStatus::getStatusFlow($orderId);

        return [
            'err' => 0,
            'data' => [
                'orderId' => $orderId,
                'status' => $order->status,
                'orderCreateTime' => $order->ctime,
                'contact' => $contact,
                'count' => $ext['count'] ?? 0,
                'email' => $ext['email'] ?? 0,
                'phone' => $ext['phone'] ?? 0,
                'useProtect' => $ext['useProtect'] ?? '',
                'statusFlow' => $statusFlow,
            ],
        ];
    }

    public static function changeStatus($orderId, $status, $reason = '') {
        $uid = Yii::$app->user->getId();
        $model = self::findOne(['order_id' => $orderId]);
        if ($uid != $model->uid) {
            return [
                'err' => 2,
                'msg' => '无此权限'
            ];
        }
        $sql = "UPDATE `order` SET `status`=$status WHERE `order_id`=$orderId";
        $affected = Yii::$app->db->createCommand($sql)->execute();
        if (!$affected) {
            return [
                'err' => 1,
                'msg' => '订单状态未改变'
            ];
        }
        $time = time();
        $ext = [];
        $ext = json_encode($ext, JSON_UNESCAPED_UNICODE);
        $sql1 = "UPDATE `order` SET `utime`=$time WHERE `order_id`=$orderId";
        $sql2 = "INSERT INTO `order_status` (`order_id`,`status`,`occur_time`,`reason`,`ext`) VALUES ($orderId,$status,$time,'$reason','$ext')";
        $transaction = Yii::$app->db->beginTransaction();
        try {
            Yii::$app->db->createCommand($sql1)->execute();
            Yii::$app->db->createCommand($sql2)->execute();
            $transaction->commit();
            return ['err' => 0];
        } catch (\Exception $e) {
            $transaction->rollBack();
            return [
                'err' => 1,
                'msg' => $e->getMessage()
            ];
        }
    }
}
